import 'package:aone_common/common.dart';
import 'package:aone_common/dto/dto.dart';
import 'package:aone_common/entities/webui/get_all_games.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

import '../../../components/v_loading.dart';
import '../../../theme/custom_theme.dart';
import '../widgets/favorite_icon_btn.dart';
import '../widgets/player_count.dart';
import '../widgets/rating_bar.dart';
import '../widgets/start_game_button.dart';

class CategoryRowView extends StatelessWidget {
  const CategoryRowView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final state = GameCategoryLogic.to.state;
    final logic = GameCategoryLogic.to;
    final favLogic = FavoriteLogic.to;

    final _theme = CustomTheme.colorSchema;
    return RefreshIndicator(
      onRefresh: () => Future.sync(
        () => state.pagingController.refresh(),
      ),
      child: Obx(
        () => PagedListView(
          padding: EdgeInsets.symmetric(horizontal: 20.w),
          pagingController: state.pagingController,
          builderDelegate: PagedChildBuilderDelegate<AllGameItem>(
            itemBuilder: (BuildContext context, item, int index) {
              return Container(
                height: 84.w,
                margin: EdgeInsets.only(bottom: 15.h),
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                    color: _theme.secondary,
                    borderRadius: BorderRadius.circular(10.0),
                    boxShadow: [CustomTheme.boxShadowSm]),
                child: Row(
                  children: [
                    CachedNetworkImage(
                      fit: BoxFit.fill,
                      width: 84.w,
                      height: 84.w,
                      imageUrl: item.imgCn!,
                    ),
                    Expanded(
                      child: Container(
                        height: 84.w,
                        padding: EdgeInsets.symmetric(
                          horizontal: 10.w,
                          vertical: 5.h,
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            Text(
                              item.nameCn!,
                              textAlign: TextAlign.start,
                              style: TextStyle(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.w500,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            Rating(heat: item.heat!),
                            PlayerCountLabel(count: item.number!),
                          ],
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12.w),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          GestureDetector(
                            child: SizedBox(
                              height: 28.h,
                              child: favoriteIcon(item.isCollect!),
                            ),
                            onTap: () async {
                              await logic.toggleFavorite(
                                payload: FavoriteDto(
                                  gameSubId: item.gameSubId,
                                  isCollect: item.isCollect,
                                ),
                                subTag: item.subTag!,
                              );
                              favLogic.getFavoriteList(noLoading: true);

                            },
                          ),
                          StartGameButton(params: item),
                        ],
                      ),
                    ),
                  ],
                ),
              );
            },
            animateTransitions: true,
            firstPageProgressIndicatorBuilder: (_) =>
                Center(child: customLoading()),
          ),
        ),
      ),
    );
  }
}
